package game.tycoon.sosial;
import java.util.ArrayList;
import java.util.Random;
import java.util.Scanner;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Point;
import android.util.Log;

public class PeopleController {

		private static final String TAG = PeopleController.class.getSimpleName();
		//titik apollo
		final Point Puncak;
		final Point Kiri_Atas;
		final Point Kiri_Bawah;
		final Point Kanan_Atas;
		final Point Kanan_Bawah;

		
		//titik buletan
		final Point pusatLingkaran;
		final int jarijari;
			
		public ArrayList<People> listOfPeople= new ArrayList<People>();//variabel penampung semua nada drum		
		private Bitmap ordPeople;

		private Bitmap nonPlayer;
		public Bitmap gundubiasa;
		private Bitmap gundu1;
		private Bitmap gundu2;
		private Bitmap gundu3;
		private Bitmap gundu4;
		private Bitmap gundu5;
		public int gesekan[][];
		public int batasGerak[][];
		public final static int TANAH_BIASA = 5;
		public final static int AIR = 10;
		public final static int BATU = 30;
		public final static int RUMPUT = 25;
		public final static int LUMPUR = 40;
		//ukuran petak layar
		private int scale_x,scale_y;//lebar dan panjang satu sel(buat gesekan)
		private int width, height;//lebar dan panjang map
		
		//status hit
		public int hit1=-9999;
		public int hit2=-9999;
		
		//animasi cipratan air
		public static Bitmap[] animwater;
		//animasi cipratan batu
		public static Bitmap[] animStone;
		//animasi cipratan rumput
		public static Bitmap[] animGrass;
		//animasi cipratan lumpur
		public static Bitmap[] animMud;
		
		//animas marker
		public static Bitmap[] animMarker;
		
		Context context;
		
	    public PeopleController(Context context,Resources res, int _width, int _height, int mode, int taruhan){
	    	width = _width;
	    	height = _height;
	    	ordPeople = BitmapFactory.decodeResource(res, R.drawable.people1);
//	    	nonPlayer = BitmapFactory.decodeResource(res, R.drawable.gundumotion1);
//	    	gundubiasa = BitmapFactory.decodeResource(res, R.drawable.gundumotion1);
//	    	gundu1 = BitmapFactory.decodeResource(res, R.drawable.gundu1);
//	    	gundu2 = BitmapFactory.decodeResource(res, R.drawable.gundu2);
//	    	gundu3 = BitmapFactory.decodeResource(res, R.drawable.gundu3);
//	    	gundu4 = BitmapFactory.decodeResource(res, R.drawable.gundu4);
//	    	gundu5 = BitmapFactory.decodeResource(res, R.drawable.gundu5);
	    	
	    	this.context = context;   
	    	
	    	animwater = new Bitmap[5];
//	    	animwater[0] = BitmapFactory.decodeResource(res, R.drawable.water_1);
//	    	animwater[1] = BitmapFactory.decodeResource(res, R.drawable.water_2); 
//	    	animwater[2] = BitmapFactory.decodeResource(res, R.drawable.water_3);
//	    	animwater[3] = BitmapFactory.decodeResource(res, R.drawable.water_4);
//	    	animwater[4] = BitmapFactory.decodeResource(res, R.drawable.water_5);
	    	animGrass = new Bitmap[5];
//	    	animGrass[0] = BitmapFactory.decodeResource(res, R.drawable.grass_1);
//	    	animGrass[1] = BitmapFactory.decodeResource(res, R.drawable.grass_2);
//	    	animGrass[2] = BitmapFactory.decodeResource(res, R.drawable.grass_3);
//	    	animGrass[3] = BitmapFactory.decodeResource(res, R.drawable.grass_4);
//	    	animGrass[4] = BitmapFactory.decodeResource(res, R.drawable.grass_5);
	    	animMud = new Bitmap[5];
//	    	animMud[0] = BitmapFactory.decodeResource(res, R.drawable.mud_1);
//	    	animMud[1] = BitmapFactory.decodeResource(res, R.drawable.mud_2);
//	    	animMud[2] = BitmapFactory.decodeResource(res, R.drawable.mud_3);
//	    	animMud[3] = BitmapFactory.decodeResource(res, R.drawable.mud_4);
//	    	animMud[4] = BitmapFactory.decodeResource(res, R.drawable.mud_5);
	    	animStone = new Bitmap[5];
//	    	animStone[0] = BitmapFactory.decodeResource(res, R.drawable.kerikil_1);
//	    	animStone[1] = BitmapFactory.decodeResource(res, R.drawable.kerikil_2);
//	    	animStone[2] = BitmapFactory.decodeResource(res, R.drawable.kerikil_3);
//	    	animStone[3] = BitmapFactory.decodeResource(res, R.drawable.kerikil_4);
//	    	animStone[4] = BitmapFactory.decodeResource(res, R.drawable.kerikil_5);
	    	//load gambar marker
	    	animMarker = new Bitmap[14];
//	    	animMarker[0] = BitmapFactory.decodeResource(res, R.drawable.c_1);
//	    	animMarker[1] = BitmapFactory.decodeResource(res, R.drawable.c_2);
//	    	animMarker[2] = BitmapFactory.decodeResource(res, R.drawable.c_3);
//	    	animMarker[3] = BitmapFactory.decodeResource(res, R.drawable.c_4);
//	    	animMarker[4] = BitmapFactory.decodeResource(res, R.drawable.c_5);
//	    	animMarker[5] = BitmapFactory.decodeResource(res, R.drawable.c_6);
//	    	animMarker[6] = BitmapFactory.decodeResource(res, R.drawable.c_7);
//	    	animMarker[7] = BitmapFactory.decodeResource(res, R.drawable.c_8);
//	    	animMarker[8] = BitmapFactory.decodeResource(res, R.drawable.c_9);
//	    	animMarker[9] = BitmapFactory.decodeResource(res, R.drawable.c_10);
//	    	animMarker[10] = BitmapFactory.decodeResource(res, R.drawable.c_11);
//	    	animMarker[11] = BitmapFactory.decodeResource(res, R.drawable.c_12);
//	    	animMarker[12] = BitmapFactory.decodeResource(res, R.drawable.c_13);
//	    	animMarker[13] = BitmapFactory.decodeResource(res, R.drawable.c_14);
	    	// init apollo
	    	Puncak = new Point(465,81);
	    	Kiri_Atas = new Point(420,165);
	    	Kiri_Bawah = new Point(420,549);
	    	Kanan_Atas = new Point(510,165);
	    	Kanan_Bawah = new Point(510,549);
	    	pusatLingkaran = new Point(470,385);
	    	jarijari = 160;
//	    	if (mode==1){ //buletan
//	    		Buletan(taruhan,140);
//	    	}
//	    	else if(mode==2){ //apollo
//	    		Apollo(taruhan);
//	    	}
//	    	else if(mode==4){ // latihan
//	    		Latihan();
//	    	}
	    	MapExtern(res);
	    }
	    
	    public void addPeople(){ // menambahkan orang
	    	listOfPeople.add(new People(ordPeople,1,300,0,0,0,0,Color.BLUE, width, height));
    		Log.d("none", "People bertambah 1");
	    }
	    public void removePeople(){ // menambahkan orang
	    	if (listOfPeople.size()>1){
	    		listOfPeople.remove(0);
	    		Log.d("none", "People berkurang 1");
	    	}
	    }
	    
	    public void ChangePeople(int id, int play){
	    	if (FirstPlayer()!=-9999){
		    	if (play==1) {
		    		//markerin player
		    		listOfPeople.get(id).marked = true;	    		
		    	}else{ 
		    		//nonmarkerin non player
		    		listOfPeople.get(id).marked = false;	    		
		    	}
	    	}
	    }
	    public boolean PeopleExist(){
	    	boolean ketemu = false;
	    	if (listOfPeople.size()!=0){
	    		ketemu = true;
	    	}
	    	return ketemu;
	    }
	    public boolean noPeopleBuletan(){
	    	boolean truthy = false;
	    	if ((!PeopleExist()) || (FirstPlayer()==0)){
	    		truthy = true;
	    	}
	    	return truthy;
	    }
	    public void PositioningApollo(){
	    	for(int i=0;i<CountPeopleNP();i++){
	       		listOfPeople.get(i).x = Puncak.x - (gundubiasa.getWidth()/2);
	       		listOfPeople.get(i).y = (int)(Kiri_Atas.y)+((i-1)*(gundubiasa.getHeight() + 10) - gundubiasa.getHeight()/2);       	
	       	}
	    }
	    public int FurthestPlayer(){
	    	int Max = listOfPeople.size()-1;
			int i = Max-1;
			while (getPeople(i).isPlayer() != 0){
				if(getPeople(i).y>=getPeople(Max).y){
					Max = i;
				}
				i-=1;
			}
			return Max;
	    }
	    public boolean NoPeopleMoving(){
	    	boolean IsMove = true;
	    	if(listOfPeople.size()>0){
		    	for (int i=0;i<listOfPeople.size();i++){
		    		if ((listOfPeople.get(i).xinc!=0)||(listOfPeople.get(i).yinc!=0)){
		    			IsMove = false;
		    		}
		    	}
	    	}
	    	return IsMove;
	    }
	    public int NearestPlayer(int akhir){ // mengembalikan indeks player yang jaraknya terpendek
	    	int Min = akhir;
			int i = Min-1;
			while (getPeople(i).isPlayer() != 0){
				if(getPeople(i).getCenterY()<getPeople(Min).getCenterY()){
					Min = i;
				}
				i-=1;
			}
			return Min;
	    }
	    public int FirstPlayer(){ //mengembalikan indeks player terkecil
			int First = 0;
			boolean ketemu = false;
			First=0;
			while ((First!=listOfPeople.size())&&(!ketemu)){
				if (getPeople(First).isPlayer() != 0){
					ketemu = true;
				}
				else{
					First+=1;
				}
			}
			if (!ketemu){
				First = -9999;
			}
			return First;
	    }
	    public void PositioningPlayer(){
	    	int last = listOfPeople.size()-1;
	    	if (last>0){
		    	int min = NearestPlayer(last);
		    	int first = FirstPlayer();
		    	People temp;
		    	while(last!=first){
		    		Log.d(TAG, "last = "+last+", min = "+min+", first="+first);   	
					temp = new People(getPeople(min).getBitmap(), getPeople(min).isPlayer(), getPeople(min).mass, (int)getPeople(min).x, (int)getPeople(min).y, 0,0, Color.CYAN, width, height);
		    		Log.d(TAG, "temp = "+temp.isPlayer()+", min="+getPeople(min).isPlayer());
					listOfPeople.remove(min);
					listOfPeople.add(temp);   		
		    		last-=1;
		    		min = NearestPlayer(last);
		    	}
		    	temp = new People(getPeople(min).getBitmap(), getPeople(min).isPlayer(), getPeople(min).mass, (int)getPeople(min).x, (int)getPeople(min).y, 0,0, Color.CYAN, width, height);
				Log.d(TAG, "temp = "+temp.isPlayer()+", min="+getPeople(min).isPlayer());
				listOfPeople.remove(min);
				listOfPeople.add(temp);
	    	}
	    }
	    
	    public int getIdPlayer(int player){ //ngembaliin indeks gundu dengan id player = player
	    	int z=0;
	    	while(getPeople(z).isPlayer()!=player){
	    		z+=1;
	    	}
	    	if(getPeople(z).isPlayer()!=player){
	    		z = -9999;
	    	}
	    	return z;
	    }
	    
	    public void Info(){
	    	for(int i=0;i<listOfPeople.size();i++){
	    		Log.d(TAG, "Bola "+i+" id="+listOfPeople.get(i).isPlayer());
	    	}
	    }
	    public void Buletan(int NumlistOfPeople, int radius){//versi main buletan
	    	int remaining = NumlistOfPeople;    	
	    	double wx, wy, distance;
	    	Random randomGenerator = new Random();
	    	// ini untuk starter buletan   		
	       	do{    		
	    		wx = randomGenerator.nextInt((jarijari - gundubiasa.getWidth()/2) - (-jarijari + gundubiasa.getWidth()/2) + 1) + -jarijari + gundubiasa.getWidth()/2;
	    		wy = randomGenerator.nextInt((jarijari - gundubiasa.getWidth()/2) - (-jarijari + gundubiasa.getWidth()/2) + 1) + -jarijari + gundubiasa.getWidth()/2;       	
		    	distance=Math.sqrt(wx*wx+wy*wy);
		    	if ((!Tubrukan((int)wx+pusatLingkaran.x,(int)wy+pusatLingkaran.y))&&(distance<(radius - gundubiasa.getWidth()/2))){
	    	    	listOfPeople.add(new People(randomizePeopleBitmapVariation(),0,300,(int)(wx+pusatLingkaran.x),(int)(wy + pusatLingkaran.y),0,0,Color.CYAN, width, height));
	    			remaining-=1;
	    		}
	    	}while((remaining!=0));
	    }
	    public void Latihan(){//versi main Latihan
	    	int remaining = 10;	
	    	double wx, wy;
	    	Random randomGenerator = new Random();
	    	// ini untuk starter buletan   		
			Log.d(TAG, "jumlah gundu sekarang = "+listOfPeople.size());
	       	do{    		
	    		wx = randomGenerator.nextInt(width);
	    		wy = randomGenerator.nextInt(height);       	
		    	if (!Tubrukan((int)wx,(int)wy)){
	    	    	listOfPeople.add(new People(randomizePeopleBitmapVariation(),0,300,(int)wx,(int)wy,0,0,Color.CYAN, width, height));
	    			remaining-=1;
	    		}
	    	}while((remaining!=0));
			wx = randomGenerator.nextInt(width);
			wy = randomGenerator.nextInt(height);       	
	    	while(FirstPlayer()==-9999){
	    		wx = randomGenerator.nextInt(width);
	    		wy = randomGenerator.nextInt(height);       	
	    		if (!Tubrukan((int)wx,(int)wy)){
	    		InsertPlayer
					(1,
					0, 0, (int)wx, (int)wy);							
	        	}
	    	}
	    }
	    public boolean Tubrukan(int center_x, int center_y){
	    	boolean kebenaran = false;
	    	if(listOfPeople.size()>0){
		    	for (int i=0;i<listOfPeople.size();i++){
		    		
		    		double wx=listOfPeople.get(i).getCenterX()-center_x;
		    		double wy=listOfPeople.get(i).getCenterY()-center_y;
			    	//colliding balls (theorem of Pythagoras)
			    	double distance=Math.sqrt(wx*wx+wy*wy);
			    	if(distance<People.diameter){
			    		kebenaran = true;
			    	}
		    	}
	    	}
	    	return kebenaran;
	    	
	    }
	    public void Apollo(int NumlistOfPeople){//versi main uleran
	    	// ini untuk starter apollo       	
	       	for(int i=0;i<NumlistOfPeople;i++){        	
	       		Log.d(TAG,"coba spawn " + i + " sukses");
	       		listOfPeople.add(new People(randomizePeopleBitmapVariation(),0,300,Puncak.x - (gundubiasa.getWidth()/2),(int)(Kiri_Atas.y)+((i-1)*(gundubiasa.getHeight() + 10) - gundubiasa.getHeight()/2),0,0,Color.CYAN, width, height));       	
	       	}
	    }
	    public void InsertPlayer(int id, int powerx, int powery, int posisiX, int posisiY){//0 = nonplayer, sisanya player
	    	listOfPeople.add(new People(nonPlayer,id,300,posisiX,posisiY,powerx,powery,Color.BLUE, width, height));   		
	    }
	    public People getPeople(int i){
	        return listOfPeople.get(i);
	    }
	    public void update(){
	    	//random add people berdasarkan waktup
	    	Random randomGenerator = new Random();
    		int randomPeople = randomGenerator.nextInt(50);
    	    if ((randomPeople==1)&&(listOfPeople.size()<=10)){
    	    	addPeople();
    	    }
	    	for (int i=0;i<listOfPeople.size();i++){
	    		//Log.d(TAG, "jumlah gundu di update = "+listOfPeople.size());
	    		//Log.d(TAG,"kordinat ("+(int) ((listOfPeople.get(i).x)/scale_x)+","+(int) ((listOfPeople.get(i)).y/scale_y)+")"+i+". gesekan="+hitungGesekan(i));    	
//	    		listOfPeople.get(i).move(hitungGesekan(i));
	    		listOfPeople.get(i).moveByOrder();	    			
	    		if (listOfPeople.get(i).doneOrder()){
		    		listOfPeople.remove(i);	    			
	    		}
	        }
	        handleCollision();
	    }

	    public int hitungGesekan(int i){         	
	    	return gesekan[(int) ((listOfPeople.get(i).x)*1.0/(width/scale_x))][(int) ((listOfPeople.get(i)).y*1.0/(height/scale_y))];
	    }
	    public void MapExtern(Resources Res){
	    	Scanner read = new Scanner(Res.openRawResource(WorldMap.getFrictionMap(WorldSetting.playWorld)));

	    	String s;
	    	if(read.hasNext()) {
				//Log.d(TAG,s);			
	    		s = read.next();
	    		Log.d(TAG,"berapa"+Integer.valueOf(s));			
	    		scale_x = Integer.valueOf(s);
	    		s = read.next();
	    		scale_y = Integer.valueOf(s);
	    		Log.d(TAG,"kali"+Integer.valueOf(s));		
	    		batasGerak = new int[scale_x][scale_y];
	        	//gesekan = new int[scale_x][scale_y];
	        	
				for(int i=0;i<scale_y;i++) {
					s = read.next();
					for(int j=0;j<scale_x;j++) {
						if(s.charAt(j)=='0') {//tanah biasa / permukaan biasa
							batasGerak[j][i] = 0;
							//gesekan[j][i] = TANAH_BIASA;
						}
						else if (s.charAt(j)=='1'){// air
							batasGerak[j][i] = 1;
			        		//gesekan[j][i] = AIR;        		
						}
//						else if (s.charAt(j)=='2'){// batu
//			        		gesekan[j][i] = BATU;        		
//						}
//						else if (s.charAt(j)=='3'){// rumput
//			        		gesekan[j][i] = RUMPUT;        		
//						}
//						else if(s.charAt(j)=='4') {// lumpur
//							gesekan[j][i] = LUMPUR;
//						}
					}
				}			
			}else{
			}
	    }
	    public int CountPeopleNP(){
	    	int jumlah=0;
	    	if (PeopleExist()){
	    		for(int z=0;z<listOfPeople.size();z++){
	    			if  (getPeople(z).isPlayer()==0){
	    				jumlah+=1;
	    			}
	    		}
	    	}
	    	return jumlah;
	    }
	    boolean collide(People b1, People b2){
	    	double wx=b1.getCenterX()-b2.getCenterX();
	    	double wy=b1.getCenterY()-b2.getCenterY();
	    	double dx, dy, collisionision_angle, magnitude_1, magnitude_2, direction_1, direction_2, new_xspeed_1, new_yspeed_1, new_xspeed_2, new_yspeed_2, final_xspeed_1, final_xspeed_2, final_yspeed_1, final_yspeed_2;
	    	//colliding balls (theorem of Pythagoras)
	    	double distance=Math.sqrt(wx*wx+wy*wy);
	    	if(distance<People.diameter){
	    		if (b1.x<b2.x){
	    			b1.x-=1;//(30-distance)/4;
	    			b2.x+=1;//(30-distance)/4;
	    		}
	    		else {
	    			b1.x+=1;//(30-distance)/4;
	    			b2.x-=1;//(30-distance)/4;
	    		}
	    		if (b1.y<b2.y){
	    			b1.y-=1;//(30-distance)/4;
	    			b2.y+=1;//(30-distance)/4;
	    		}
	    		else {
	    			b1.y+=1;//(30-distance)/4;
	    			b2.y-=1;//(30-distance)/4;
	    		}
	    		dx = b1.getCenterX()- b2.getCenterX();
	    		dy =  b1.getCenterY()- b2.getCenterY();
	    		collisionision_angle = Math.atan2(dy, dx);
	    		magnitude_1 = Math.sqrt(b1.xinc * b1.xinc + b1.yinc * b1.yinc);
	    		magnitude_2 = Math.sqrt(b2.xinc * b2.xinc + b2.yinc*b2.yinc);
	    		direction_1 = Math.atan2(b1.yinc, b1.xinc);
	    		direction_2 = Math.atan2(b2.yinc, b2.xinc);
	    		new_xspeed_1 = magnitude_1 * Math.cos(direction_1 - collisionision_angle);
	    		new_yspeed_1 = magnitude_1 * Math.sin(direction_1 - collisionision_angle);
	    		new_xspeed_2 = magnitude_2 * Math.cos(direction_2 - collisionision_angle);
	    		new_yspeed_2 = magnitude_2 * Math.sin(direction_2 - collisionision_angle);
	    		final_xspeed_1 = ((b1.mass - b2.mass) * new_xspeed_1 + (b2.mass + b2.mass) * new_xspeed_2) / (b1.mass + b2.mass);
	    		final_xspeed_2 = ((b1.mass + b1.mass) * new_xspeed_1 + (b2.mass - b1.mass) * new_xspeed_2) / (b1.mass + b2.mass);
	    		final_yspeed_1 = new_yspeed_1;
	    		final_yspeed_2 = new_yspeed_2;
	    		b1.xinc = Math.cos(collisionision_angle) * final_xspeed_1 + Math.cos(collisionision_angle + 1.570796E+000) * final_yspeed_1;
	    		b1.yinc = Math.sin(collisionision_angle) * final_xspeed_1 + Math.sin(collisionision_angle + 1.570796E+000) * final_yspeed_1;
	    		b2.xinc = Math.cos(collisionision_angle) * final_xspeed_2 + Math.cos(collisionision_angle + 1.570796E+000) * final_yspeed_2;
	    		b2.yinc = Math.sin(collisionision_angle) * final_xspeed_2 + Math.sin(collisionision_angle + 1.570796E+000) * final_yspeed_2;
	    		b1.xinc = b1.xinc*0.98;
	    		b1.yinc = b1.yinc*0.98;
	    		b2.xinc = b2.xinc*0.98;
	    		b2.yinc = b2.yinc*0.98;
	    		return true;
	    	}
	    	return false;
	    }
	    public int CountPeopleOut(){
	    	int Jumlah=0;
	    	int temp=0;
	    	double wx;
	    	double wy;
	    	//colliding balls (theorem of Pythagoras)
	    	double distance;
	    	while (getPeople(temp).isPlayer()==0){
	    		wx=pusatLingkaran.x-getPeople(temp).getCenterX();
	    		wy=pusatLingkaran.y-getPeople(temp).getCenterY();
	    		distance=Math.sqrt(wx*wx+wy*wy);
	    		if (distance>(jarijari)){ // lebih dari radius, gundu dihapus
	    			Jumlah+=1;
	    		}
	    		temp+=1;
	    	}
	    	Log.d(TAG,"jumlah gundu di luar = "+Jumlah);
	    	return Jumlah;
	    }
	    public void RemovePeopleOut(){    	
	    	int temp=0;
	    	double wx;
	    	double wy;
	    	//colliding balls (theorem of Pythagoras)
	    	double distance;
	    	while (getPeople(temp).isPlayer()==0){    		
	    		wx=(pusatLingkaran.x)-getPeople(temp).getCenterX();
	    		wy=(pusatLingkaran.y)-getPeople(temp).getCenterY();
	    		distance=Math.sqrt(wx*wx+wy*wy);
	    		if (distance>(jarijari)){ // lebih dari radius dihapus
	    			listOfPeople.remove(temp);
	    			Log.d(TAG,"hapus gundu ke = "+temp);
	    		}
	    		else{
	    			temp+=1;
	    		}
	    	}    	
	    }
	    
	    
	    public boolean listOfPeopleinApollo(People B){
	    	boolean aman = false;
	    	int kiri, kanan, atas, bawah;
			kiri = Kiri_Atas.x;
			kanan = Kanan_Atas.x;
			atas = Puncak.y;
			bawah = Kanan_Bawah.y;
//			Coordinate a = new Coordinate(Kiri_Atas.x, Kiri_Atas.y);
//			Coordinate b = new Coordinate(Kanan_Atas.x, Kanan_Atas.y);
//			Coordinate c = new Coordinate(Puncak.x, Puncak.y);
//			Coordinate d = new Coordinate(B.x, B.y);
			
//			if(((B.x<=kanan)&&(B.x>=kiri)&&(B.y>=atas)&&(B.y<=bawah)) || (Utility.checkPointInsideTriangle(a, b, c, d))){
				aman = true;
//			}
			
	    	return aman;
	    }

	    public int CheckApolloNP(){
	    	int retval = -9999;
	    	if(CountPeopleNP()>0){
	        	Log.d(TAG, "Count People NP="+CountPeopleNP());
	    		for(int z=0;z<CountPeopleNP();z++){
	    			if ((retval==(-9999))&&(!listOfPeopleinApollo(getPeople(z)))){
	    				retval = z;
	    			}
	    		}
	    	}
	    	return retval;
	    }
	    public void PutBackPlayer(){
			for(int z=0;z<listOfPeople.size();z++){
				if ((listOfPeople.get(z).y<165) && (listOfPeople.get(z).isPlayer()!=0)){
					listOfPeople.get(z).y = (height/3)+650;
				}
			}
	    }
	    public int CheckApolloP(){
	    	int retval=-9999;
	    	if((listOfPeople.size()-CountPeopleNP())>0){        	
	    		Log.d(TAG, "First Player = "+FirstPlayer());
	    		for(int z=FirstPlayer();z<listOfPeople.size();z++){
	    			if ((retval==(-9999))&&(listOfPeopleinApollo(getPeople(z)))){
	    				retval = z;
	    			}
	    		}
	    	}
	    	return retval;
	    }
	    public int CheckPlayer(){
	    	int Jumlah=0;
	    	int temp=FirstPlayer();
	    	double wx;
	    	double wy;
	    	//colliding balls (theorem of Pythagoras)
	    	double distance;
			while (temp!=listOfPeople.size()){
	    		wx=pusatLingkaran.x-getPeople(temp).getCenterX();
	    		wy=pusatLingkaran.y-getPeople(temp).getCenterY();
	    		distance=Math.sqrt(wx*wx+wy*wy);
	    		if (distance<=(jarijari)){ //+radius
	    			listOfPeople.remove(temp);
	    			Log.d(TAG,"hapus gundu ke = "+temp);
	    			Jumlah+=1;
	    		}
	    		else{
	    			temp+=1;
	    		}
	    	}
			return Jumlah;
	    }
	    public int PlayerOut(){
	    	int Indeks=FirstPlayer();
	    	boolean ketemu = false;
	    	double wx;
	    	double wy;
	    	//colliding balls (theorem of Pythagoras)
	    	double distance;
	    	if(Indeks!=-9999){
				while ((Indeks!=listOfPeople.size())&&(!ketemu)){
					Log.d(TAG, "indeks = "+Indeks);
					wx = pusatLingkaran.x-getPeople(Indeks).getCenterX();
					wy = pusatLingkaran.y-getPeople(Indeks).getCenterY();
		    		distance=Math.sqrt(wx*wx+wy*wy);
		    		if (distance<=(jarijari)){ //+radius
		    			ketemu = true;
		    			Log.d(TAG, "KETEMU -9999 di = "+Indeks);
		    		}else{
		    			Indeks+=1;
		    		}
		    	}
				if (!ketemu){
					Indeks = -9999;
				}
	    	}
			return Indeks;
	    }
	    public int PeopleOutOfBuletan(){
	    	double wx;
	    	double wy;
	    	int z=-9999;
	    	//colliding balls (theorem of Pythagoras)
	    	double distance;
	    	boolean ketemu = false;
	    	if (CountPeopleNP()>0){
	    		z=0;
	    		while((z!=CountPeopleNP())&&(!ketemu)){
		    		wx=(pusatLingkaran.x)-getPeople(z).getCenterX();
		    		wy=(pusatLingkaran.y)-getPeople(z).getCenterY();
		    		distance=Math.sqrt(wx*wx+wy*wy);
		    		if (distance>(jarijari)){ //+radius
		    			ketemu = true;
		    			Log.d(TAG, "KETEMU di = "+z);
		    			Log.d(TAG, "kordinat x= "+getPeople(z).getCenterX()+", kordinat y = "+getPeople(z).getCenterY());
		    		}else{
		    			z+=1;
		    		}
	    		}
	    		if (!ketemu){
	    			z = -9999;
	    		}
	    	}
	    	return z;
	    }
	    
	    public boolean PlayerInBuletan(int id){
	    	boolean radius = false;
	    	double wx;
	    	double wy;
	    	//colliding balls (theorem of Pythagoras)
	    	double distance;
	  		wx=(pusatLingkaran.x)-getPeople(id).getCenterX();
	   		wy=(pusatLingkaran.y)-getPeople(id).getCenterY();
	  		distance=Math.sqrt(wx*wx+wy*wy);
	   		if (distance<=(jarijari)){ //+radius
	   			radius = true;
	   			Log.d(TAG, "KETEMU radius");
	  		}
	   		return radius;
	    }
	    
	    private void handleCollision(){
	    	People temp;
	    	//we iterate through all the balls, checking for collision
	    	for(int i=0;i<listOfPeople.size();i++){
	    		for(int j=0;j<listOfPeople.size();j++){
	    			if(i!=j)
	    			{
	    				if(collide(listOfPeople.get(i), listOfPeople.get(j))){    					
	    					GameSound.startTabrakan(context);    					
	    					hit1 = i;
	    					hit2 = j;
	    					temp = listOfPeople.get(i);
	    					listOfPeople.get(i).hit(listOfPeople.get(j));
	    					listOfPeople.get(j).hit(temp);
	    				}
	    			}
	    		}
	        }
	    }
	    
	    //ngerandom variasi gundu yang muncul di layar hape
	    //seberapa banyak variasi gundu yang dimunculin tergantung unlock level terbesar player-player yang sedang main
	    private Bitmap randomizePeopleBitmapVariation() {
	    	Random random = new Random();
	    	int maxVar = 0;
	    	//maksimum variasi adalah maksimum variasi dari seluruh player
	    	for(int i=0;i<WorldSetting.PlayingEmployee.size();i++) {
	    		if(maxVar < WorldSetting.PlayingEmployee.get(i).unlockLevel) {
	    			maxVar = WorldSetting.PlayingEmployee.get(i).unlockLevel;
	    		}
	    	}
	    	int hasilVarPeople = random.nextInt(maxVar+1);
	    	switch(hasilVarPeople) {
	    	case 0:
	    		return gundubiasa;
	    	case 1:
	    		return gundu1;
	    	case 2:
	    		return gundu2;
	    	case 3: 
	    		return gundu3;
	    	case 4:
	    		return gundu4;
	    	case 5:
	    		return gundu5;
	    	default:
	    		return gundubiasa;
	    	}
	    }

}
